package net.hzlj.platform.enterprise.helper;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Vibrator;

import net.hzlj.platform.enterprise.BootApp;

/**
 * @Description: 铃音管理
 * @Author: Jason Zhang
 * @Date: 2016/11/28 0028
 */

public class RingtoneManager {
    private static final RingtoneManager instance = new RingtoneManager();

    private MediaPlayer mMediaPlayer;

    private Vibrator vibrator;

    public static RingtoneManager getInstance() {
        return instance;
    }

    private RingtoneManager() {

    }

    public void play(){
        try {
            Uri alert = android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_RINGTONE);
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(BootApp.getAppContext(), alert);
//final AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            vibrator = (Vibrator) BootApp.getAppContext().getSystemService(Context.VIBRATOR_SERVICE);
            long[] pattern = {800, 150, 400, 130}; // OFF/ON/OFF/ON...
            vibrator.vibrate(pattern, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop(){
        try {
            if(this.mMediaPlayer != null) {
                if(mMediaPlayer.isPlaying()) {
                    this.mMediaPlayer.stop();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (null != vibrator) {
                vibrator.cancel();
                vibrator = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
